#include "Cmn.h"
#include "MyWnd.h"
BEGIN_MESSAGE_MAP(MyWnd, CFrameWnd)
	ON_WM_PAINT()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_DESTROY()
	ON_COMMAND(ID_START_SIGNUP, &MyWnd::OnStartSignup)
	ON_COMMAND(ID_START_SIGNIN, &MyWnd::OnStartSignin)
	ON_COMMAND(ID_START_SIGNOUT, &MyWnd::OnStartSignout)
	ON_COMMAND(ID_EDIT_ADDEVENT, &MyWnd::OnEditAddevent)
	ON_COMMAND(ID_EDIT_C, &MyWnd::OnEditC)
	ON_COMMAND(ID_EDIT_CANCEL, &MyWnd::OnEditCancel)
	ON_COMMAND(ID_FRIENDS_ADDFRIEND, &MyWnd::OnFriendsAddfriend)
	ON_COMMAND(ID_FRIENDS_SHOWFRIEND, &MyWnd::OnFriendsShowfriend)
	ON_COMMAND(ID_FRIENDS_FRIENDREMOVE, &MyWnd::OnFriendsFriendremove)
	ON_COMMAND(ID_FRIENDS_SHOWREQUESTS, &MyWnd::OnFriendsShowrequests)
	ON_COMMAND(ID_FRIENDS_CANCELREQUEST, &MyWnd::OnFriendsCancelrequest)
END_MESSAGE_MAP()
MyWnd::MyWnd(void)
{
	day_selected=0;
	_month_=2;
	_year_=2013;
	Create(NULL,"Calender GUI");
	menu.LoadMenu(IDR_MENU1);
	SetMenu(&menu);
}
void MyWnd:: OnDraw(CDC& DC){
	X.paint(DC,70,30,50);
	if(CmdClient._logged_){
		CPen P1(PS_SOLID, 5, RGB(0, 0, 255));
		CPen* pOldPen = (CPen*)DC.SelectObject(&P1);
		DC.SetTextColor(RGB(200,0,0));
		DC.TextOutA(800,5,"logged in");
		DC.TextOut(900,5,CmdClient.getUserName().c_str());
		DC.SelectObject(pOldPen);
	}
}
void MyWnd:: OnPaint(){
	CPaintDC DC(this);
	CRect R;GetClientRect(&R);
	CDC MDC;MDC.CreateCompatibleDC(&DC);
	CBitmap BMP;
	BMP.CreateCompatibleBitmap(&DC, R.Width(), R.Height());
	MDC.SelectObject(BMP);
	MDC.PatBlt(0, 0, R.Width(), R.Height(), WHITENESS);
	OnDraw(MDC);
	DC.BitBlt(0, 0, R.Width(), R.Height(), &MDC, 0, 0, SRCCOPY);
}
void MyWnd::OnLButtonDown(UINT nFlags,CPoint Point){
	int Cx,Cy;
	if(X.OnButtonClicked(Point.x,Point.y)){
		Invalidate(1);
	}
}
void MyWnd::OnMouseMove(UINT nFlags,CPoint Point){
	if(X.CrossingBy(Point.x,Point.y))Invalidate(0);
}
afx_msg void MyWnd:: OnDestroy()
{
	PostQuitMessage(0);
}
MyWnd::~MyWnd(void)
{
}

void MyWnd::OnStartSignup()
{
	CSignUpDialog Dlg;
	if(Dlg.DoModal() == IDOK){
		if(Dlg.Password==Dlg.Repeat_Password){
			if(CmdClient.SignUp((LPCTSTR)Dlg.User_Name,(LPCTSTR)Dlg.Password)){

				MessageBox("Congratulations");
			}
			else
			{
				MessageBox("Sorry Failed");
			}
		}
		else	MessageBox("Passwords don't match");
	}
	// TODO: Add your command handler code here
}

void MyWnd::OnStartSignin()
{
	CSignInDialog Dlg;
	if(Dlg.DoModal()==IDOK){
		if(CmdClient.login((LPCTSTR)Dlg.User_Name,(LPCTSTR)Dlg.Password)){

			MessageBox("Logged In");
		}
		else
		{
			MessageBox("Sorry Failed");
		}
	}
}
void MyWnd::OnStartSignout()
{
	if(CmdClient.signOut()){
		MessageBox("Sign Out successfuly");
	}
	else MessageBox("Failed");
}


void MyWnd::OnEditAddevent()
{
	CAddEvent Dlg;
	if(Dlg.DoModal()==IDOK){
		CString temp=Dlg.Starting_time_day+"/"+Dlg.Starting_time_month+"/"+Dlg.Starting_time_year;
		CString temp1=Dlg.Starting_time_Hour+":"+Dlg.Starting_time_Minutes;
		CString temp2=Dlg.Ending_time_day+"/"+Dlg.Ending_time_Month+"/"+Dlg.Ending_time_Year;
		CString temp3=Dlg.Ending_time_Hour+":"+Dlg.Ending_time_Minutes;
		if(CmdClient.addevent((LPCTSTR)temp1,(LPCTSTR)temp,(LPCTSTR)temp3,(LPCTSTR)temp2,(LPCTSTR)Dlg.Event_Name)){
		MessageBox("event added");
		}
		else MessageBox("Failed");
	}
}


void MyWnd::OnEditC()
{
	CEditEventDialog Dlg;
	if(Dlg.DoModal()==IDOK){
		CString temp=Dlg.Starting_Time_day+"/"+Dlg.Starting_Time_Month+"/"+Dlg.Starting_time_year;
		CString temp1=Dlg.Starting_time_Hour+":"+Dlg.Starting_time_minutes;
		CString temp2=Dlg.Ending_time_day+"/"+Dlg.Ending_time_month+"/"+Dlg.Ending_time_day;
		CString temp3=Dlg.Ending_time_Hour+":"+Dlg.Ending_time_Minutes;
		CmdClient.changeEventBeginning((LPCTSTR)Dlg.Event_name,(LPCTSTR)temp1,(LPCTSTR)temp);
		CmdClient.changeEventEnd((LPCTSTR)Dlg.Event_name,(LPCTSTR)temp3,(LPCTSTR)temp2);
	}
	
	// TODO: Add your command handler code here
}


void MyWnd::OnEditCancel()
{
	CCancelEventDialog Dlg;
	if(Dlg.DoModal()==IDOK){
		CmdClient.deleteevent((LPCTSTR)Dlg.Event_Name);
	}
	// TODO: Add your command handler code here
}


void MyWnd::OnFriendsAddfriend()
{
	CAddFriendDialog Dlg;
	if(Dlg.DoModal()==IDOK){
		CmdClient.Add_Frind(CmdClient.getUserName(),(LPCTSTR)Dlg.Friend_Name);
	}
	// TODO: Add your command handler code here
}


void MyWnd::OnFriendsShowfriend()
{
	CShowFriendDialog Dlg;
	Dlg.enterData(CmdClient.ShowFrinds(CmdClient.getUserName()));
	Dlg.DoModal();
	// TODO: Add your command handler code here
}


void MyWnd::OnFriendsFriendremove()
{
	CRemoveFriendDialog Dlg;
	if(Dlg.DoModal()==IDOK){
		CmdClient.Remove_Frind(CmdClient.getUserName(),(LPCTSTR)Dlg.Friend_name);
	}
	// TODO: Add your command handler code here
}


void MyWnd::OnFriendsShowrequests()
{
	// TODO: Add your command handler code here
}

void MyWnd::OnFriendsCancelrequest()
{
	// TODO: Add your command handler code here
}
